﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;

public partial class Whir_System_Module_Config_Sitemap : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        RoleHelper.IsLogin();    //登录判断
    }
    protected void btSave_Click(object sender, EventArgs e)
    {
        string dTime = string.Format("{0:yyyy-MM-ddThh:mmzzz}", DateTime.Now);
        AddXML(tbUrl.Text.Trim(), tbYx.Text.Trim(), dTime, DropDownList1.SelectedValue);
    }

    /// <summary>
    /// 插入SITEMAP数据
    /// </summary>
    /// <param name="_webUrl"></param>
    /// <param name="_priority"></param>
    /// <param name="lastTime"></param>
    /// <param name="freq"></param>
    private void AddXML(string _webUrl, string _priority, string lastTime, string freq)
    {

        XmlNamespaceManager nsmgr;
        string path = Server.MapPath("~\\sitemap\\sitemap.xml");
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load(path);
        nsmgr = new XmlNamespaceManager(xmldoc.NameTable);

        nsmgr.AddNamespace("tk", "http://www.google.com/schemas/sitemap/0.84");

        XmlNode root = xmldoc.SelectSingleNode("//tk:urlset", nsmgr);

        XmlElement xe1 = xmldoc.CreateElement("", "url", xmldoc.DocumentElement.NamespaceURI);//创建一个<url>节点        

        XmlElement xesub1 = xmldoc.CreateElement("", "loc", xmldoc.DocumentElement.NamespaceURI);
        xesub1.InnerText = _webUrl;//设置文本节点
        xe1.AppendChild(xesub1);//添加到<loc>节点中

        XmlElement xesub3 = xmldoc.CreateElement("", "lastmod", xmldoc.DocumentElement.NamespaceURI);
        xesub3.InnerText = lastTime;
        xe1.AppendChild(xesub3);

        XmlElement xesub4 = xmldoc.CreateElement("", "changefreq", xmldoc.DocumentElement.NamespaceURI);
        xesub4.InnerText = freq;
        xe1.AppendChild(xesub4);

        XmlElement xesub2 = xmldoc.CreateElement("", "priority", xmldoc.DocumentElement.NamespaceURI);
        xesub2.InnerText = _priority;
        xe1.AppendChild(xesub2);

        root.AppendChild(xe1);//添加到<urlset>节点中    

        xmldoc.Save(Server.MapPath("~\\sitemap\\sitemap.xml"));
    }

    protected void btnDown_Click(object sender, EventArgs e)
    {
        string filePath = Server.MapPath("~\\sitemap\\sitemap.xml");
        string fileName = Path.GetFileName(filePath);
        Response.ContentType = "APPLICATION/OCTET-STREAM";
        Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
        Response.WriteFile(filePath);
        Response.End();
    }
}
